package com.xy.blog.article.domain.controller;

import com.xy.blog.article.api.SpecialColumnService;
import com.xy.blog.article.api.model.SpecialColumnDTO;
import com.xy.blog.article.api.model.SpecialColumnQuery;
import com.xy.blog.article.domain.mapstruct.SpecialColumnConverter;
import com.xy.blog.article.domain.service.ISpecialColumnService;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: 专栏
 * @date 2020/7/17
 */
@Api(value = "ArticleController",tags = "专栏")
@RestController
public class SpecialColumnController extends BaseController implements SpecialColumnService {

    @Autowired
    private ISpecialColumnService specialColumnServiceImpl;

    @Autowired
    private SpecialColumnConverter specialColumnConverter;

    @Override
    public Object pages(PageReq<SpecialColumnQuery> req) throws BaseException {
        return getPageData(specialColumnServiceImpl.listByPage(req));
    }

    @Override
    public Object getById(Long id) throws BaseException {
        return getData(specialColumnServiceImpl.getById(id));
    }

    @PostMapping(BASE_PATH)
    public Object save(MultipartFile file, SpecialColumnDTO req) throws BaseException, IOException {
        specialColumnServiceImpl.save(specialColumnConverter.dto2Bo(req),file.getBytes(),file.getOriginalFilename());
        return success();
    }

    @Override
    public Object update(@Valid SpecialColumnDTO req) throws BaseException {
        specialColumnServiceImpl.update(specialColumnConverter.dto2Bo(req));
        return success();
    }

    @Override
    public Object deleteById(Long id) throws BaseException {
        specialColumnServiceImpl.deleteById(id);
        return success();
    }

    @Override
    public Object list(String name) throws BaseException {
        return getData(specialColumnServiceImpl.list(name));
    }

    @Override
    public Object view(Long id) throws BaseException {
        specialColumnServiceImpl.view(id);
        return success();
    }
}
